
create trigger Tr_OrderDetail
	on [order Details]
	for insert 
as 
	declare @idP int, @cant int, @stock int

	select @idP = inserted.productId, @cant = inserted.Quantity
	from inserted

	select @stock = UnitsInStock
	from Products
	where ProductID = @idP
	

	if (@stock-@cant <= 0)
	begin
	    raiserror('no hay suficiente producto',16,1)
	end
	else
	begin
	update Products set UnitsInStock = @stock - @cant
		where ProductID = @IdP
	end